package com.wyl.xue.admin.system.mybatis.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyl.xue.admin.system.mybatis.entity.SystemRoles;
import com.wyl.xue.admin.system.mybatis.entity.SystemUserRole;
import com.wyl.xue.admin.system.mybatis.mapper.SystemRolesMapper;
import com.wyl.xue.admin.system.mybatis.service.SystemRoleMenuService;
import com.wyl.xue.admin.system.mybatis.service.SystemRolesService;
import com.wyl.xue.admin.system.mybatis.service.SystemUserRoleService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: SystemRolesServiceImpl
 * @Function: 角色
 * @Date: 2019/12/18 22:14
 * @author wyl
 * @version V1.0
 */
@Service
@AllArgsConstructor
@Slf4j
@DS("master")
public class SystemRolesServiceImpl extends ServiceImpl<SystemRolesMapper, SystemRoles> implements SystemRolesService {
    final SystemRoleMenuService systemRoleMenuService;
    final SystemUserRoleService systemUserRoleService;

    /**
     * @Description 通过用户ID 获取该用的所有权限
     * @param userId 用户Id
     * @return java.util.List<com.wyl.xue.system.mybatis.entity.SystemRoles>
     * @Date 2020/4/13 17:37
     * @Author wangyl
     * @Version V1.0
     */
    public List<SystemRoles> getRolesByUserId(Long userId) {
        List<SystemUserRole> systemUserRoles = systemUserRoleService.list(Wrappers.<SystemUserRole>lambdaQuery().eq(SystemUserRole::getUserId, userId));
        List<Long> roleIds = systemUserRoles.parallelStream()
                                            .map(SystemUserRole::getRoleId)
                                            .collect(Collectors.toList());
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        return listByIds(roleIds);
    }

}
